const vm = new Vue({
    el: '#eformapp',
    data: {
        formJson: {"widgetList":[],"formConfig":{"labelWidth":80,"labelPosition":"left","size":"","labelAlign":"label-left-align","cssCode":"","customClass":"","functions":"","layoutType":"PC","onFormCreated":"","onFormMounted":"","onFormDataChange":"","onFormValidate":""}},
        formData: {},
        optionData: {},
        templateId: '',
        dataId: '',
        dataLinkTable: '',
        // 显示默认的提交按钮
        showDefaultSubmitButton: false
    },
    //https://www.yuque.com/variantdev/vform/pi1mff#mE6tm
    mounted() {
        // 初始化表单配置和数据
        this.dataId = T.p('dataId');
        let that = this;
        //从后端接口获取已保存的数据对象，并赋值给formData变量
        $.ajax({
            async: false,
            url: T.webRoot + '/eform/eformdata/info/' + this.dataId,
            type: 'GET',
            success: function(res) {
                if (res.code === 0) {
                    let newFormData = $.parseJSON(res.data.dataJson);
                    $.ajax({
                        async: false,
                        url: T.webRoot + '/eform/eformtemplate/info/' + res.data.templateId,
                        type: 'GET',
                        success: function(res) {
                            let templateConfig = $.parseJSON(res.data.templateConfig);
                            that.$refs.vFormRef.setFormJson(templateConfig);
                            that.$nextTick(() => {
                                that.$refs.vFormRef.setFormData(newFormData);
                            });
                        },
                        error: function(err) {
                            console.log('err', err)
                        }
                    });
                } else {
                    console.log('加载数据失败！');
                }
            },
            error: function(err) {
                console.log('err', err)
            }
        });
    },
    methods: {
        // 表单提交
        defaultSubmitForm: function() {
            let that = this;
            this.$refs.vFormRef.getFormData().then( function(formData) {
                // Form Validation OK
                console.log('submitForm1', JSON.stringify(formData));
                let data = {
                    'templateId': that.templateId,
                    'dataLinkTable': that.dataLinkTable,
                    'dataJson': JSON.stringify(formData),
                };
                $.ajax({
                    url: 'eform/eformdata/save',
                    type: 'post',
                    contentType: 'application/json',
                    data: JSON.stringify(data),
                    success: function(res) {
                        layer.msg(res.msg, {icon: 6});
                        window.close();
                    },
                    error: function(err) {
                        layer.msg('网络异常，请稍后重试！', {icon: 5});
                    }
                });
            }).catch( function(error) {
                // Form Validation Failed
                alert(error)
            })
        }
    }
});